package com.web.service.remark;

import com.web.bean.remark.Answer;
import com.web.bean.remark.Remark;
import com.web.dao.remark.RemarkDaoImpl;
import com.web.utils.DBUtil;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

public class RemarkService {
    /*添加评论*/
    public void addToNovelService(Remark remark) throws Exception{
        Connection connection= DBUtil.getInstance().getConnection();
        RemarkDaoImpl remarkDaoImpl=new RemarkDaoImpl(connection);
        remarkDaoImpl.addRemarkForNovel(remark);
    }
    /*根据nid查找对应的小说总评*/
    public List<Remark> searchRemarkForNoveById(String nid) throws Exception{
        Connection connection= DBUtil.getInstance().getConnection();
        RemarkDaoImpl remarkDaoImpl=new RemarkDaoImpl(connection);
        List<Remark> remarks=remarkDaoImpl.selectRemarkForNovelById(nid);
        return remarks;
    }
    /*查找可用的rid*/
    public Integer searchFreeId() throws Exception{
        Connection connection= DBUtil.getInstance().getConnection();
        RemarkDaoImpl remarkDaoImpl=new RemarkDaoImpl(connection);
        List<String> str_rids=remarkDaoImpl.selectAllRid();
        List<Integer> tmp_int_rids=new ArrayList<>();
        for (int i=0;i<str_rids.size();i++){
            tmp_int_rids.add(Integer.parseInt(str_rids.get(i)));
        }
        return tmp_int_rids.get(tmp_int_rids.size()-1)+1;
    }
    /*根据rid删除章节评论*/
    public void deleteRemarkForChapterByRid(String rid) throws Exception{

    }

    public void deleteRemarkForNoveByRid(String rid)throws Exception{
        Connection connection=DBUtil.getInstance().getConnection();
        RemarkDaoImpl remarkDaoImpl=new RemarkDaoImpl(connection);
        remarkDaoImpl.deleteRemarkById(rid);
    }
    public List<Remark> selectRemarkForChapter(String nid,String chapterid) throws Exception{
        Connection connection=DBUtil.getInstance().getConnection();
        RemarkDaoImpl remarkDaoImpl=new RemarkDaoImpl(connection);
        List<Remark> remarks=remarkDaoImpl.selectRemarksForChapter(nid,chapterid);
        return remarks;
    }
    public Integer searchFreeAid() throws  Exception{
        Connection connection=DBUtil.getInstance().getConnection();
        RemarkDaoImpl remarkDaoImpl=new RemarkDaoImpl(connection);
        List<String> str_aids=remarkDaoImpl.selectAllAid();
        List<Integer> tmp_int_aids=new ArrayList<>();
        for (int i=0;i<str_aids.size();i++){
            tmp_int_aids.add(Integer.parseInt(str_aids.get(i)));
        }
        return tmp_int_aids.get(tmp_int_aids.size()-1)+1;
    }

    public void addReply(Answer answer) throws Exception{
        Connection connection=DBUtil.getInstance().getConnection();
        RemarkDaoImpl remarkDaoImpl=new RemarkDaoImpl(connection);
        String rid=answer.getRid();
        String replyCount=remarkDaoImpl.selectReplyCount(rid).toString();
        remarkDaoImpl.updateReplyCount(rid,replyCount);
        remarkDaoImpl.addReply(answer);
    }
    public List<Answer> selectAnswerService(String rid) throws Exception{
        Connection connection=DBUtil.getInstance().getConnection();
        RemarkDaoImpl remarkDaoImpl=new RemarkDaoImpl(connection);
        List<Answer> answers=new ArrayList<>();
        answers=remarkDaoImpl.selectAnswer(rid);
        return answers;
    }
}
